﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// News 的摘要说明
/// </summary>
public class News
{
    private int id;

    public int Id
    {
        get { return id; }
        set { id = value; }
    }
    private int classid;

    public int Classid
    {
        get { return classid; }
        set { classid = value; }
    }


    private string title;

    public string Title
    {
        get { return title; }
        set { title = value; }
    }
    private string author;

    public string Author
    {
        get { return author; }
        set { author = value; }
    }
    private int suggest;

    public int Suggest
    {
        get { return suggest; }
        set { suggest = value; }
    }
    private string keyword;

    public string Keyword
    {
        get { return keyword; }
        set { keyword = value; }
    }
    private string pic;

    public string Pic
    {
        get { return pic; }
        set { pic = value; }
    }
    private string content;

    public string Content
    {
        get { return content; }
        set { content = value; }
    }
    private DateTime addtime;

    public DateTime Addtime
    {
        get { return addtime; }
        set { addtime = value; }
    }
    private int hit;

    public int Hit
    {
        get { return hit; }
        set { hit = value; }
    }

    private string lasttitle;

    public string Lasttitle
    {
        get { return lasttitle; }
        set { lasttitle = value; }
    }


    public const string ADDNEWS = "insert into news values(@classid,@title,@Author,@suggest,@keyword,@pic,@content,@addtime,@hit,@Lasttitle)";

    public const string UPDATENEWSBYID = "update news set classid=@classid,title=@title,keyword=@keyword,pic=@pic,content=@content,addtime=@addtime where id=@id";

    public const string GETALLNEWS = "select a.*,b.classname from news as a left join class as b on a.classid=b.classid order by a.id desc";

    public const string DELNEWSBYID = "delete from news where id=@id";

    public const string GETNEWSINFOBYID = "select * from news where id=@id";

    public const string ADDHIT = "update news set hit=hit+1 where id=@id";

    public const string GETNEWSBYCLASSID = "select * from news where classid=@classid";

    public const string GETCONTENTBYCLASSID = "select top 1 content from news where classid=@classid order by id desc";

    public const string GETCONTENT = "select top 1 * from news where classid = (select classid from class where parentid=@pid and classname=@cname) order  by id desc ";

    public string GetContentByCid(int classid)
    {
        try
        {
            return SqlHelper.ExecuteScalar(GETCONTENTBYCLASSID,
                new SqlParameter("@classid", classid)).ToString();
        }
        catch
        {
            return "";
        }
    }

    public DataTable GetNewsByCid(int id)
    {
        DataTable dt = new DataTable();
        SqlHelper.FillDataTable(GETNEWSBYCLASSID, dt,
            new SqlParameter("@classid", id));
        return dt;
    }

    public void AddHit(int id)
    {
        SqlHelper.ExecuteNonQuery(ADDHIT,
            new SqlParameter("@id", id));
    }



    public void AddNews(News n)
    {
        SqlHelper.ExecuteNonQuery(ADDNEWS,
            new SqlParameter("@classid", n.Classid),
            new SqlParameter("@title", n.Title),
            new SqlParameter("@Author", n.Author),
            new SqlParameter("@suggest", n.Suggest),
            new SqlParameter("@keyword", n.Keyword),
            new SqlParameter("@pic", n.Pic),
            new SqlParameter("@content", n.Content),
            new SqlParameter("@addtime", n.Addtime),
            new SqlParameter("@hit", n.Hit),
            new SqlParameter("@Lasttitle", n.lasttitle));
    }

    public void UpdateNews(News n)
    {
        SqlHelper.ExecuteNonQuery(UPDATENEWSBYID,
            new SqlParameter("@classid", n.Classid),
            new SqlParameter("@title", n.Title),
            new SqlParameter("@keyword", n.Keyword),
            new SqlParameter("@pic", n.Pic),
            new SqlParameter("@content", n.Content),
            new SqlParameter("@addtime", n.Addtime),
            new SqlParameter("@id", n.Id));
    }

    public void DelNews(int id)
    {
        SqlHelper.ExecuteNonQuery(DELNEWSBYID, new SqlParameter("@id", id));
    }

    public DataTable GetAllNews()
    {
        DataTable dt = new DataTable();
        SqlHelper.FillDataTable(GETALLNEWS, dt);
        return dt;
    }

    public DataTable GetNewsById(int id)
    {
        DataTable dt = new DataTable();
        SqlHelper.FillDataTable(GETNEWSINFOBYID, dt,
            new SqlParameter("@id", id));
        return dt;
    }

    public string GetContentByPid(int pid, string cname)
    {
        DataTable dt = new DataTable();
        SqlHelper.FillDataTable(GETCONTENT, dt,
            new SqlParameter("@pid", pid),
            new SqlParameter("@cname", cname));
        if (dt.Rows.Count > 0)
        {
            return dt.Rows[0]["content"].ToString();
        }
        else
        {
            return "资料正在整理中...";
        }
    }

}
